﻿using DAL;
using IBLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;
using NPOI.XSSF.UserModel;

namespace WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HotelController : ControllerBase
    {
        public HotelController(IHotelBLL bll,HotelDbContext context,ILogger<HotelController> logger)
        {
            Bll = bll;
            Context = context;
            Logger = logger;
        }

        public IHotelBLL Bll { get; }
        public HotelDbContext Context { get; }
        public ILogger<HotelController> Logger { get; }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="m">职员信息表</param>
        /// <returns>返回匿名函数</returns>
        [HttpPost]
        public async Task<int> HAdd(Hotel m)
        {
            try
            {
                Logger.LogInformation("添加职员成功");
                return await Bll.HAdd(m);
            }
            catch (Exception ex)
            {
                Logger.LogError($"添加职员异常{ex.Message}");
                throw ex;
            }
            
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="lx">职员类型</param>
        /// <returns>通过条件查询来返回数据</returns>
        [HttpGet]
        public IQueryable<Hotel> HShow(int? lx, string? name)
        {
            try
            {
                Logger.LogInformation("职员显示成功");
                return Bll.HShow(lx, name);
            }
            catch (Exception ex)
            {
                Logger.LogError($"职员显示异常{ex.Message}");
                throw ex;
            }
            
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <returns>导出全部数据</returns>
        [HttpGet]
        public IActionResult Index()
        {
            List<Hotel> hotel=Context.hotels.ToList();
            XSSFWorkbook wb = new XSSFWorkbook();
            var sheet= wb.CreateSheet("职员管理表");
            var row= sheet.CreateRow(0);
            row.CreateCell(0).SetCellValue("姓名");
            row.CreateCell(1).SetCellValue("职位类型");
            row.CreateCell(2).SetCellValue("性别");
            row.CreateCell(3).SetCellValue("身份证号");
            row.CreateCell(4).SetCellValue("手机号");
            row.CreateCell(5).SetCellValue("管理小区");

            int a = 1;
            foreach (var b in hotel)
            {
                var rows = sheet.CreateRow(a);
                rows.CreateCell(0).SetCellValue(b.Hotelname);
                rows.CreateCell(1).SetCellValue(b.HotelLX==1?"项目经理":(b.HotelLX == 2 ? "财务总监": "普通职员"));
                rows.CreateCell(2).SetCellValue(b.Hotelsex==true?"男":"女");
                rows.CreateCell(3).SetCellValue(b.Hotelzheng);
                rows.CreateCell(4).SetCellValue(b.HotelPhone);
                rows.CreateCell(5).SetCellValue(b.Hotelxq);
                a++;
            }
            MemoryStream memoryStream = new MemoryStream();
            wb.Write(memoryStream);
            byte[] data = memoryStream.ToArray();
            string mime = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider().Mappings[".xlsx"];
            return File(data,mime, "职员管理表.xlsx");


        }
    }
}
